<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>产品溯源</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <link rel="stylesheet" href="css/iconfont.css" />
	    <link rel="stylesheet" href="css/capacity.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/product_tracklist.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	    <script type="text/javascript" src="js/template.js" ></script>
	    <script type="text/javascript" src="js/companydata.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
	    <section class="AllContent" style="background-color: #fff;">
	    	<div id="piclist">
	    		<div style="overflow: hidden;" id="content">
					<img style="width: 40%;margin:20px auto;display: block;" src="images/loading5.gif">
				</div>
			</div>
	    </section>
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/bootstrap-paginator.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	    mui.init({
	    	
	    });
	    
		$(function () {
			$('.AllHeader li:eq(0)').removeClass('mui-action-back mui-pull-left').addClass('tiaozhuan');
			$('.AllHeader li:eq(1)').html("<a class='companyTy' href='CompanyTraceability.html'>公司</a><a class='productTy'>产品</a>");
			$('.AllHeader li:eq(2)').html('<img onclick="addProductTrack();" class="theme-login" style="width:22px" src="images/addproducts.png">');
			$('.titleTwo').css({lineHeight:'30px',marginTop:'5px'})
	        $('.ChothingTtyTitle').hide();
            $('.titleTwo a').css({color:'#fff',backgroundColor:'#00aafa'});
            $('.titleTwo a:eq(1)').css({color:'#0088CC',backgroundColor:'#fff'});
            
			$('.tiaozhuan').on('tap',function(){
				plus.webview.getWebviewById('CompanyTraceability.html').close();
				window.location.href= 'supper.html';
			});
//          $(".companyTy").on('tap',function(){
//				mui.openWindow({
//					url: 'CompanyTraceability.html', 
//					id:'CompanyTraceability.html',
//					show:{
//				    	aniShow:'fade-in'//页面显示动画，默认为”slide-in-right“；
//				    }
//				});
//			})
            
            /*遮罩*/
            $('.theme-login').click(function(){
				$('.theme-popover-mask').fadeIn(100);
				$('.theme-popover').slideDown(200);
				$('.AllContent').css({overflow:'hidden'});
			})
			$('.theme-popover-mask,.exit,.guanbi').click(function(){
				$('.theme-popover-mask').fadeOut(100);
				$('.theme-popover').slideUp(200);
				$('.AllContent').css({overflowY:'scroll'});
			})
			
	        document.addEventListener('deviceready', onDeviceReady, true);
	        loadProductTrack();
		});
	
	    function onDeviceReady() {
	        document.addEventListener("backbutton", onBackKeyDown, false);
	    }
	    function onBackKeyDown() {
	        openUrl("index.html");
	    }
	    function queryProduct() {
	        var q = $("#query").val();
	        if (empty(q)) {
	            loadProductTrack();
	        } else {
	            var datas = {access_token:getToken(),page_no:getPageNo(),pname:q};
	            $.ajax({
	                url:getServerUrl()+"/Open/ComSys/sourcelist",
	                type:'post',
	                dataType:'json',
	                data: datas,
	                success:function(result){
	                    var rData =  result;
	                    rData.SiteUrl = getwindowhref();
	                    rData.Role = getUserRole();
	                    var html = template.render('home_body',rData);
	                    $("#content").html(html);
	                    var Idenity = getIdentity();
	                    if(Idenity == '企业'){
	                        var homepage = 'supper.html';
	                    }else if(Idenity == '网点'){
	                        var homepage = 'website.html';
	                    }
	                    $('#idheader').append('<p class="h4">'
	                            +'<a href="'+homepage+'"> <i class="fa fa-home"></i> <span>首页</span></a>&nbsp;>>&nbsp;'
	                            + '产品默认溯源</p>'
	                            +'<ul class="nav navbar-nav navbar-right">'
	                            +'<li class="dropdown">'
	                            + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">'
	                            + '<span class="thumb-sm avatar pull-left m-t-n-xs m-r-xs"><img src="style/images/avatar_default.jpg"></span>'
	                            +'系统设置<b class="caret"></b>'
	                            + '</a>'
	                            +'<ul class="dropdown-menu animated fadeInLeft">'
	                            + '<li><a href="company_pwd.html">修改密码</a></li>'
	                            + '<li><a href="company_manage.html">用户管理</a></li>'
	                            + '<li><a href="companyconfig.html">系统参数</a></li>'
	                            +'</ul>'
	                            +'</li>'
	                            + '<li><a href="#" onclick="logout();"> <span>注销</span></a></li>'
	                            +'</ul>'
	                    );
	                    showpage(rData.total_results,rData.page_no,rData.page_size,getwindowhref());
	                }
	            });
	
	        }
	    }
	    function  addTracks() {
	        var pid = $("#product_name").val();
	        var pname = $("#product_name").find("option:selected").text();
	        var from_branch = $("#from_branch").val();
	        var to_branch = $("#to_branch").val();
	        var start_date = $("#start_date").val();
	        var end_date = $("#start_date").val();
	        if (empty(pid)) {
	            alertMessage("标签ID不能为空！");
	            return;
	        }
	        if (empty(from_branch) || from_branch == 0 ) {
	            alertMessage("请选择发货网点！");
	            return;
	        }
//		        if (empty(to_branch) || to_branch == 0 ) {
//		            alertMessage("请选择目的网点！");
//		            return;
//		        }
	        if (empty(start_date)) {
	            alertMessage("时间不能为空！");
	            return;
	        }
	        var datas = {access_token:getToken(),pid:pid,from_branch:from_branch,to_branch:to_branch,start_date:start_date,end_date:end_date};
	        $.ajax({
	            type: "post",
	            url: getServerUrl() + "/Open/ComSys/addsource",
	            dataType: 'json',
	            data: datas,
	            success: function (json) {
	                if (json.result == "1") {
	                    var chtml = '';
	                    chtml = chtml + '<ul>';
                    	chtml = chtml + '<li>' + pname + '</li>';
                    	chtml = chtml + '<li><a href="product_tracking.html?pid=' + pid + '">溯源</a><a onclick="removeProduct(\'' + pid + '\');">删除</a></li>';
                    	chtml = chtml + '</ul>';
	                    
	                    $("#tracklist").prepend(chtml);
	                    $('.theme-popover-mask').fadeOut(100);
	                    $('.theme-popover').slideUp(200);
	                    $('body').css({overflowY:'scroll'});
	                    $(".modal-body input").val("");
						var strwt = $('.SKheightImg').width();
						$('.SKheightImg').height(strwt/2);
	                   
	                } else {
	                    alertMessage("添加产品溯源错误:[" + json.error_description + "]");
	                }
	            }
	        });
	    }
	
	    function removeProduct(pid) {
	        var r = confirm("请确认是否删除产品溯源?")
	        if (r === true)
	        {
	            $.ajax({
	                type: "post",
	                url: getServerUrl() + "/Open/ComSys/delProTrack",
	                dataType: 'json',
	                data:"access_token="+getToken()+ '&pid=' + pid,
	                success: function (json) {
	                    if (json.result == "1") {
	                        alertMessage("删除产品溯源完成！");
	                        loadProductTrack();
	                    } else {
	                        alertMessage("删除产品溯源错误:[" + json.error_description + "]");
	                    }
	                }
	            });
	        }
	
	    }
	    function manage(id,name){
	        setBranchId(id);
	        setCookie("wms.branchname",name);
	        openUrl("BranchUser.html");
	    }
	</script>
	
	<script type="text/html" id="home_body">
		<!-- 内容区 -->
		<div class="ContentVessel">
			<section>
	    		<!-- 输入DIV -->
				<div class="theme-popover" style="top:98px;">
					
					<!-- 标题 -->
		        	<div class="InputNavigation">
		        		<p>公司溯源</p>
		        		<p class="exit"><img src="images/remove1.png"></p>
		        	</div>
		        	
		        	<!-- 商品信息   -->
		        	<div class="AddList">
		        		<div class="CommodityInfo">
		        			<div class="FormStyle">
		        				<ul style="width: 90%;">
			        				<li>商品：</li>
			        				<li>
										<select name="product_name" id="product_name">
	                                        <option value="0">选择商品</option>
	                                    </select>
			        				</li>
			        				<li>网点：</li>
			        				<li>
										<select name="from_branch" id="from_branch">
	                                        <option value="0">选择网点</option>
	                                    </select>
			        				</li>
			        				<li>时间：</li>
			        				<li><input type="date" style="display: block;margin-top:4px;line-height:30px;" name="start_date" id="start_date" placeholder="格式：1000-00-00 00:00"/></li>
		        				
		        				</ul>
			        			
			        			<button class="NextButton" type="button" onclick="addTracks();">保存</button>
		        			</div>
		        		</div>
		        	</div>
				</div>
				
				<!-- 遮罩层 -->
				<div class="theme-popover-mask"></div>
	    	</section>
			
			<!-- 中心块  -->
			<div class="CentralBlock">
				<!-- 网点列表 -->
				<div class="CommodityList" id="tracklist">
                    <%if(list != null){%>
                    <%for(var i=0;i< list.length;i++){%>
                    	<ul><a href="product_tracking.html?pid=<%=list[i].pid%>">
                    		<li><%=list[i].pname%></li>
                    		<li><i>溯源</i><i onclick="removeProduct('<%=list[i].pid%>')">删除</i></li>
                    	</a></ul>
                    <%}%>
                    <%}%>
				</div>
			</div>
			
			<!-- 分页器 -->
			<div style="overflow: hidden;display: none;">
				<!-- 分页区 -->
				<div class="pull-right padder">
                    <ul class="pagination" id="pageUl"></ul>
                </div>
			</div>
		</div>
	</script>
	<script type="text/html" id="productlist_body">
	<%if(productlist != null){%>
	<%for(i=0;i<productlist.length;i++){%>
	    <option value="<%=productlist[i].pid%>"><%=productlist[i].name%></option>
	<%}%>
	<%}else{%>
	<option value="0">没有可添加的产品</option>
	<%}%>
	</script>
</html>
